<template>
  <div class="app-container">
    <!-- 搜索 -->
    <div class="search-container searchBox" ref="search">
      <span class="hint" style="margin-left: 15px">申请标题</span>
      <el-input
        size="default"
        clearable
        v-model="searchForm.keywords"
        class="input"
        placeholder="请输入标题"
      ></el-input>
      <span class="hint">状态</span>
      <el-select
        size="default"
        style="margin-right: 20px; width: 10%"
        v-model="searchForm.is_verify"
        clearable
        placeholder="请选择"
      >
        <el-option v-for="item in statusList" :key="item" :label="item.label" :value="item.value"> </el-option>
      </el-select>
      <el-button size="default" class="searchBtn" type="primary" @click="searchBtn">搜索</el-button>
      <el-button size="default" type="warning" @click="refreshBtn">重置</el-button>
    </div>
    <el-card shadow="never">
      <template #header>
        <el-button size="default" type="primary" @click="hanldeShowDialog">添加</el-button>
        <div class="red">
          <div>《中华人民共和国消防法》第二十一条规定：  </div>
          <div>因施工等特殊情况需要使用明火作业的，应当按照规定事先办理审批手续，采取相应的消防安全措施；作业人员应当遵守消防安全规定。进行电焊、气焊等具有火灾危险作业的人员和自动消防系统的操作人员，必须持证上岗，并遵守消防安全操作规程。</div>
        </div>
      </template>
      <el-table
        :height="pageHeight + 'px'"
        :data="tableData"
        style="width: 100%"
        v-loading="tableDataLoading"
        :default-sort="{ prop: 'id', order: 'ascending' }"
      >
        <el-table-column width="80" label="id" prop="id" sortable></el-table-column>
        <el-table-column label="申请标题" prop="title"> </el-table-column>
        <el-table-column label="所属单位" prop="unit_name"> </el-table-column>
        <el-table-column label="联系人" prop="contact_name"> </el-table-column>
        <el-table-column label="联系电话" prop="contact_phone"> </el-table-column>
        <!-- <el-table-column label="申请报告" prop="images">
          <template #default="scope">
            <span v-if="scope.row.images.length >= 1" style="color: #67c23a">已上传</span>
            <span v-else style="color: #f56c6c">未上传</span>
          </template>
        </el-table-column> -->
        <el-table-column label="人员信息" prop="personnel"> </el-table-column>
        <el-table-column label="开始时间" prop="sta_time"> </el-table-column>
        <el-table-column label="结束时间" prop="end_time"> </el-table-column>
        <el-table-column label="状态" prop="is_verify">
          <template #default="scope">
            <el-tag v-if="scope.row.is_verify == 1">已审核</el-tag>
            <el-tag v-else-if="scope.row.is_verify == 2" type="warning">审核未通过</el-tag>
            <el-tag v-else type="danger">待审核</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="area_verify_time">
          <template #default="scope">
            <el-button @click="lookLog(scope.row)" size="default" :type="scope.row.is_verify != 1 ? 'primary' : ''">{{
              scope.row.is_verify == 1 ? '查看' : '审核'
            }}</el-button>
            <el-button @click="hanldeEdit(scope.row)" size="default">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          :current-page="pagination.page"
          :total="total"
          :page-size="pagination.size"
          :page-sizes="pagination.sizes"
          :layout="pagination.layout"
          :hide-on-single-page="false"
          class="pagination"
          background
          @size-change="onSizeChange"
          @current-change="onCurrentChange"
        />
      </div>
    </el-card>

    <el-dialog title="添加" v-model="dialogVisible" width="600px" center>
      <div class="hide-box">
        <el-form ref="formData" :model="form" :rules="rules" label-width="100px" size="default">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入标题"></el-input>
          </el-form-item>
          <el-form-item label="人员信息" prop="personnel">
            <el-input v-model="form.personnel" type="textarea" placeholder="请输入人员信息"></el-input>
          </el-form-item>
          <el-form-item label="联系人" prop="contact_name">
            <el-input v-model="form.contact_name" placeholder="请输入联系人"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="contact_phone">
            <el-input v-model="form.contact_phone" placeholder="请输入联系电话"></el-input>
          </el-form-item>
          <el-form-item label="申请内容" prop="content">
            <el-input v-model="form.content" type="textarea" placeholder="请输入申请内容"></el-input>
          </el-form-item>
          <el-form-item label="申请备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入申请备注"></el-input>
          </el-form-item>
          <el-form-item label="地点" prop="address">
            <el-input v-model="form.address" type="textarea" placeholder="请输入地点"></el-input>
          </el-form-item>
          <el-form-item label="开始时间" prop="sta_time">
            <el-date-picker
              style="width: 100%"
              value-format="YYYY-MM-DD"
              v-model="form.sta_time"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间" prop="end_time">
            <el-date-picker
              style="width: 100%"
              value-format="YYYY-MM-DD"
              v-model="form.end_time"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="证书" prop="certificate">
            <CustomUpload v-model="form.certificate"></CustomUpload>
          </el-form-item>
          <el-form-item label="申请图片" prop="images">
            <!-- <CustomUpload v-model="form.images"></CustomUpload> -->
            <myImagesUpload :defaultImages="form.images"></myImagesUpload>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="hanldeConfirmSave">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
// 分页
import paginationMixin from '@/mixins/pagination'
import CustomUpload from '@/components/CustomUpload/index.vue'
import myImagesUpload from '@/components/myImagesUpload/index.vue'
export default {
  // 分页
  mixins: [paginationMixin],
  components: { CustomUpload, myImagesUpload },
  data() {
    return {
      rules: {
        title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
        personnel: [{ required: true, trigger: 'blur', message: '请输入人员信息' }],
        contact_name: [{ required: true, trigger: 'blur', message: '请输入联系人' }],
        contact_phone: [{ required: true, trigger: 'blur', message: '请输入联系电话' }],
        content: [{ required: true, trigger: 'blur', message: '请输入申请内容' }],
        remark: [{ required: true, trigger: 'blur', message: '请输入申请备注' }],
        address: [{ required: true, trigger: 'blur', message: '请输入地点' }],
        sta_time: [{ required: true, trigger: 'change', message: '请输入开始时间' }],
        end_time: [{ required: true, trigger: 'change', message: '请输入结束时间' }]
      },
      upDataUrl: import.meta.env.DEV ? '/proxy/' : import.meta.env.VITE_APP_API_BASEURL,
      tableData: [],
      tableDataLoading: false,
      //   area_name: '',
      // 总条数
      total: 0,
      searchForm: {
        keywords: '',
        is_verify: ''
      },
      searchParams: {},
      // 审核状态
      statusList: [
        { label: '未审核', value: 0 },
        { label: '已审核', value: 1 },
        { label: '审核失败', value: 2 }
      ],
      pageHeight: 0,
      dialogVisible: false,
      form: {}
    }
  },
  mounted() {
    this.pageHeight = window.innerHeight - this.$refs.search.clientHeight - 310
  },
  created() {
    this.getDataList()
  },
  methods: {
    hanldeEdit(row) {
      this.form = {
        ...row
      }
      this.dialogVisible = true
      //   this.form.images = row.images.split(',')
      //   this.form.certificate = row.certificate.split(',')
    },
    hanldeConfirmSave() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          let params = {
            ...this.form,
            images: this.form.images
          }
          this.$api.post('/unit/FirePower/save', params).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.msg)
              this.dialogVisible = false
              this.getDataList()
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
    // 打开弹窗添加修改
    hanldeShowDialog() {
      this.dialogVisible = true
    },
    getDataList() {
      this.tableDataLoading = true
      let params = {
        rows: this.pagination.size,
        page: this.pagination.page,
        ...this.searchParams
      }
      this.$api
        .post('/unit/FirePower/lists', params)
        .then((res) => {
          this.tableData = res.data.lists
          this.total = res.data.total
        })
        .finally(() => {
          this.tableDataLoading = false
        })
    },
    // 查看日志
    lookLog(row) {
      this.$router.push({
        name: 'thermalPowerLog',
        query: {
          id: row.id
        }
      })
    },
    // 搜索
    searchBtn() {
      this.pagination.page = 1
      this.searchParams = this.searchForm
      this.getDataList()
    },
    // 重置
    refreshBtn() {
      this.pagination.page = 1
      this.searchParams = {}
      this.searchForm = {
        keywords: '',
        is_verify: ''
      }
      this.getDataList()
    },
    // 时间转换
    formatDate(timeStamp, format) {
      if (!timeStamp) return ''
      timeStamp = +timeStamp
      var date = new Date(timeStamp)
      // 年月日
      var year = date.getFullYear()
      var month = date.getMonth() + 1
      month = month < 10 ? '0' + month : month
      var day = date.getDate()
      day = day < 10 ? '0' + day : day
      // 时分秒
      var hour = date.getHours()
      hour = hour < 10 ? '0' + hour : hour
      var min = date.getMinutes()
      min = min < 10 ? '0' + min : min
      var sec = date.getSeconds()
      sec = sec < 10 ? '0' + sec : sec
      if (format) {
        format = format.replace('Y', year)
        format = format.replace('M', month)
        format = format.replace('D', day)
        format = format.replace('h', hour)
        format = format.replace('m', min)
        format = format.replace('s', sec)
        return format
      } else {
        return year + '-' + month + '-' + day
      }
    }
  }
}
</script>
<style scoped lang="scss">
.block {
  margin-top: 10px;
  display: flex;
  justify-content: center;

  :deep(.el-pagination__total) {
    margin-left: 10px;
  }
}

.searchBox {
  display: flex;
  background-color: #fff;
  padding: 20px;
  box-sizing: border-box;

  .input {
    width: 10%;
    margin-right: 15px;
  }

  .hint {
    line-height: 40px;
    margin-right: 10px;
  }

  .searchBtn {
    background-color: #2d8cf0;
    color: #fff;
  }
}

.hide-box {
  height: 500px;
  overflow-y: scroll;
}

.red {
  color: red;
  font-size: 14px;
  margin-top: 10px;
}
</style>
